package com.china.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.china.adapter.BoKeAdapter;
import com.china.adapter.ZiXunAdapter;
import com.china.base.BaseFragment;
import com.china.osourcechina.R;
import com.china.osourcechina.ZiXunActivity;
import com.china.utils.AddressUtils;
import com.china.utils.HttpUtils;
import com.china.view.RefreshLayout;
import com.china.vo.Blog;
import com.china.vo.MyOschina;
import com.china.vo.Myoschinas;
import com.china.vo.News;
import com.thoughtworks.xstream.XStream;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/11.
 */
public class ZiXunFragment extends BaseFragment{
    private int catalog;
    private RefreshLayout myRefreshListView;
    private int current = 0 ;
    private String datas;
    private List<News> list = new ArrayList<News>();
    private List<News> array = new ArrayList<News>();
    private ListView listView;
    private String titles;
    private List<Blog> blogList = new ArrayList<Blog>();
    private List<Blog> blogarray = new ArrayList<Blog>();

    @Override
    protected int getLoadViewId() {
        array.clear();
        blogarray.clear();
        return R.layout.acivity_redian;
    }

    @Override
    protected void initView(View view) {
        Bundle bundles = getArguments();
        catalog= Integer.parseInt(bundles.getString("id"));
        myRefreshListView = (RefreshLayout)view.
                findViewById(R.id.swipe_layout);
        listView = (ListView)view. findViewById(R.id.listview);
        // 设置下拉刷新时的颜色值,颜色值需要定义在xml中
        myRefreshListView
                .setColorSchemeResources(R.color.circle_bule,
                        R.color.circle_green, R.color.circle_red,
                        R.color.circle_orange);

    }

    @Override
    protected void initData() {
     if (catalog == 0){
         titles="资讯详情";
         String url = AddressUtils.information + "?catalog=" + 1 + "&pageIndex=" + current + "&pageSize=" + 20 ;
        datas = HttpUtils.get(url);
         prasexml();
     }else if (catalog == 1){
         titles="资讯详情";
         String url = AddressUtils.hot+"?catalog="+ 4 + "&pageIndex=" + 0 + "&pageSize=" + 20 + "&show=week";
         datas = HttpUtils.get(url);
         prasexml();
     }else if(catalog==2){
         titles="博客详情";
         String url = AddressUtils.blog+"?type=latest"+ "&pageIndex="+ current+"&pageSize="+20;
         datas = HttpUtils.get(url);
         prasexmls();
     }else if(catalog==3){
         titles="博客详情";
         String url = AddressUtils.blog_recommended + "?type=recommend" +"&pageIndex="+current+"&pageSize=20";
         datas = HttpUtils.get(url);
         prasexmls();
     } else {
         Toast.makeText(getActivity(),"暂无数据",Toast.LENGTH_LONG).show();
     }
        myRefreshListView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                myRefreshListView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 更新完后调用该方法结束刷新
                        array.clear();
                        blogarray.clear();
                        current = 0;
                        initData();
                        myRefreshListView.setRefreshing(false);
                    }
                },1000);
            }
        });
        myRefreshListView.setOnLoadListener(new RefreshLayout.OnLoadListener(){

            @Override
            public void onLoad() {
                myRefreshListView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        current ++ ;
                        initData();
                        // 加载完后调用该方法
                        myRefreshListView.setLoading(false);
                    }
                },1500);
            }
        });

    }

    private void prasexmls() {
        XStream xs = new XStream();
        xs.processAnnotations(Myoschinas.class);
        Myoschinas oss = (Myoschinas)xs.fromXML(datas);
        blogList = oss.getBlogs().getBlog();
        blogarray.addAll(blogList);
        BoKeAdapter adapter = new BoKeAdapter(getActivity(),blogarray);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(),ZiXunActivity.class);
                intent.putExtra("titleBar",titles);
                intent.putExtra("id",blogarray.get(position).getId());
                startActivity(intent);
            }
        });
    }

    private void prasexml() {
        XStream x = new XStream();
        x.processAnnotations(MyOschina.class);
        MyOschina os =(MyOschina)x.fromXML(datas);
        list = os.getNewslist().getNews();
        array.addAll(list);
        ZiXunAdapter adapter = new ZiXunAdapter(getActivity(),array);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(),ZiXunActivity.class);
                intent.putExtra("titleBar",titles);
                intent.putExtra("id",array.get(position).getId());
                startActivity(intent);
            }
        });
    }

}
